//
//  UIView+THLine.m
//  MengLiao
//
//  Created by xiuxin on 2022/7/15.
//  Copyright © 2022 BaseBusiness. All rights reserved.
//

#import "UIView+THLine.h"

static UIColor * defaultColor = nil;

@implementation UIView (THLine)

+ (UIColor *)defaultColor
{
    if (defaultColor == nil)
    {
        defaultColor = ColorFromHex(0xE9E9E9);
    }
    return defaultColor;
}

+ (void)setDefaultColor:(UIColor *)color
{
    if (color)
    {
        defaultColor = color;
    }
}

- (UIView *)kTopLine
{
    return [self kAddTopLineWithInsets:UIEdgeInsetsZero];
}

- (UIView *)kTopLineWithColor:(UIColor *)color
{
    UIView * line = [self kTopLine];
    line.backgroundColor = color ?: [self.class defaultColor];
    return line;
}

- (UIView *)kBottomLine
{
    return [self kAddBottomLineWithInsets:UIEdgeInsetsZero];
}

- (UIView *)kBottomLineWithColor:(UIColor *)color
{
    UIView * line = [self kBottomLine];
    line.backgroundColor = color ?: [self.class defaultColor];
    return line;
}

- (UIView *)kAddTopLine:(CGFloat)left
{
    return [self kAddTopLineWithInsets:UIEdgeInsetsMake(0, left, 0, 0)];
}

- (UIView *)kAddBottomLine:(CGFloat)left
{
    return [self kAddBottomLineWithInsets:UIEdgeInsetsMake(0, left, 0, 0)];
}

- (UIView *)kTopLineAt:(CGPoint)origin
{
    return [self kAddTopLineWithInsets:UIEdgeInsetsMake(origin.y, origin.x, 0, 0)];
}

- (UIView *)kBottomLineAt:(CGPoint)origin
{
    return [self kAddBottomLineWithInsets:UIEdgeInsetsMake(origin.y, origin.x, 0, 0)];
}

- (UIView *)kAddTopLineWithInsets:(UIEdgeInsets)insets
{
    return [self kAddTopLineWithInsets:insets color:[self.class defaultColor]];
}

- (UIView *)kAddBottomLineWithInsets:(UIEdgeInsets)insets
{
    return [self kAddBottomLineWithInsets:insets color:[self.class defaultColor]];
}

- (UIView *)kAddLeftLineWithInsets:(UIEdgeInsets)insets
{
    return [self kAddLeftLineWithInsets:insets color:[self.class defaultColor]];
}

- (UIView *)kAddTopLineWithInsets:(UIEdgeInsets)insets color:(UIColor *)color
{
    UIView * line = [UIView new];
    line.backgroundColor = color ?: [self.class defaultColor];
    [self addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(insets.top);
        make.left.mas_equalTo(insets.left);
        make.right.mas_equalTo(-insets.right);
        make.height.mas_equalTo(KLineHeight);
    }];
    return line;
}

- (UIView *)kAddBottomLineWithInsets:(UIEdgeInsets)insets color:(UIColor *)color
{
    UIView * line = [UIView new];
    line.backgroundColor = color ?: [self.class defaultColor];
    [self addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-insets.bottom);
        make.left.mas_equalTo(insets.left);
        make.right.mas_equalTo(-insets.right);
        make.height.mas_equalTo(KLineHeight);
    }];
    return line;
}

- (UIView *)kAddLeftLineWithInsets:(UIEdgeInsets)insets color:(UIColor *)color
{
    UIView * line = [UIView new];
    line.backgroundColor = color ?: [self.class defaultColor];
    [self addSubview:line];
    [line mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(insets.top);
        make.bottom.mas_equalTo(-insets.bottom);
        make.left.mas_equalTo(insets.left);
        make.width.mas_equalTo(KLineHeight);
    }];
    return line;
}

- (UIView *)kAddRightLineWithInsets:(UIEdgeInsets)insets color:(UIColor *)color
{
    UIView * line = [UIView new];
    line.backgroundColor = color ?: [self.class defaultColor];
    [self addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(insets.top);
        make.bottom.mas_equalTo(-insets.bottom);
        make.right.mas_equalTo(-insets.right);
        make.width.mas_equalTo(KLineHeight);
    }];
    return line;
}

- (UIView *)kAddRectWithEdge:(UIEdgeInsets)insets
{
    UIView * line = [UIView new];
    line.backgroundColor = [UIColor clearColor];
    line.userInteractionEnabled = NO;
    line.layer.borderWidth = KLineHeight;
    line.layer.borderColor = [self.class defaultColor].CGColor;
    [self addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(insets.left);
        make.top.mas_equalTo(insets.top);
        make.bottom.mas_equalTo(-insets.bottom);
        make.right.mas_equalTo(-insets.right);
    }];
    return line;
}

- (UIView *)kAddRectWithEdge:(UIEdgeInsets)insets radius:(CGFloat)radius
{
    UIView * line = [UIView new];
    line.backgroundColor = [UIColor clearColor];
    line.userInteractionEnabled = NO;
    line.layer.borderWidth = KLineHeight;
    line.layer.borderColor = [self.class defaultColor].CGColor;
    line.layer.cornerRadius = radius;
    line.clipsToBounds = YES;
    [self addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(insets.left);
        make.top.mas_equalTo(insets.top);
        make.bottom.mas_equalTo(-insets.bottom);
        make.right.mas_equalTo(-insets.right);
    }];
    return line;
}

- (UIView *)kAddTopLine:(CGFloat)left tag:(NSInteger)tag
{
    [[self viewWithTag:tag] removeFromSuperview];
    UIView * line = [self kAddTopLine:left];
    line.tag = tag;
    return line;
}

- (UIView *)kAddBottomLine:(CGFloat)left tag:(NSInteger)tag
{
    [[self viewWithTag:tag] removeFromSuperview];
    UIView * line = [self kAddBottomLine:left];
    line.tag = tag;
    return line;
}

- (UIView *)kTopLine:(NSInteger)tag
{
    [[self viewWithTag:tag] removeFromSuperview];
    UIView *line = [self kTopLine];
    line.tag = tag;
    return line;
}

- (UIView *)kBottomLine:(NSInteger)tag
{
    [[self viewWithTag:tag] removeFromSuperview];
    UIView *line = [self kBottomLine];
    line.tag = tag;
    return line;
}

- (UIView *)kTopLineWithLeft:(CGFloat)left color:(UIColor *)color
{
    return [self kAddTopLineWithInsets:UIEdgeInsetsMake(0, left, 0, 0) color:color ?: [self.class defaultColor]];
}

- (UIView *)kBottomLineWithLeft:(CGFloat)left color:(UIColor *)color
{
    return [self kAddBottomLineWithInsets:UIEdgeInsetsMake(0, left, 0, 0) color:color ?: [self.class defaultColor]];
}

//#####################具体应用
/// 无间距1px线条
- (UIView *)kBottomLineWithGrayColor
{
    return [self kBottomLineWithColor:ColorFromHex(0xdedede)];
}

@end
